package com.itlnn.xfhttp;

import com.itlnn.xfhttp.able.HttpListener;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;

/**
 * 网络请求的server类，此类的作用就是初始化请求队列，处理各种请求队列
 * @User :      xuyuqiang
 * @Date :      2016/12/27 下午2:02
 * @Version :   1.0.0
 * @ClassName : XfHttpRequestServer
 **/
public class XfHttpRequestServer {

    private static XfHttpRequestServer instance ;

    private RequestQueue mRequestQueue ;

    private XfHttpRequestServer(){
        this.mRequestQueue = NoHttp.newRequestQueue() ;
    }

    public synchronized static XfHttpRequestServer getInstance(){
        if (instance == null){
            instance = new XfHttpRequestServer() ;
        }
        return  instance ;
    }

    /**
     * 添加请求的request到请求队列中
     * @param what        请求标记
     * @param request     请求的request
     * @param callback    请求的监听回调
     */
    public <T> void addRequest(int what, Request<T> request, HttpListener<T> callback) {
        mRequestQueue.add(what, request, new HttpResponse<T>(callback));
    }

    /**
     * 取消这个sign标记的所有请求
     */
    public void cancelBySign(Object sign) {
        mRequestQueue.cancelBySign(sign);
    }

    /**
     * 取消队列中所有请求
     */
    public void cancelAll() {
        mRequestQueue.cancelAll();
    }

    /**
     * 退出app时停止所有请求
     */
    public void stopAll() {
        mRequestQueue.stop();
    }

}
